

package com.zf.yichat.controller.mission;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.response.MissionDto;
import com.zf.yichat.mapper.MissionApiMapper;
import com.zf.yichat.mapper.MissionMapper;
import com.zf.yichat.model.Mission;
import com.zf.yichat.model.User;
import com.zf.yichat.model.UserBalanceDetail;
import com.zf.yichat.service.BalanceService;
import com.zf.yichat.service.MissionService;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.BalanceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 一句话描述功能
 *
 * @author fengsong
 * @date create in 11:08 2019/7/9 2019
 */
@Controller
@RequestMapping("mission")
public class MissionController extends BaseController {


    @Autowired
    private MissionService missionService;

    @Autowired
    private MissionMapper missionMapper;

    @Autowired
    private MissionApiMapper missionApiMapper;

    @Autowired
    private BalanceService balanceService;

    @Autowired
    private UserService userService;


    @RequestMapping("/index")
    public String index() {
        return "mission/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Long userId, String title, Integer status) {


        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(missionApiMapper.selectList(userId, title, status), copy -> {
            MissionDto dto = new MissionDto();
            User user = userService.selectById(copy.getUserId());
            dto.setNick(user.getNick());
            dto.setUserId(copy.getUserId());
            dto.setId(copy.getId());
            dto.setTitle(copy.getTitle());
            dto.setMemo(copy.getMemo());
            dto.setMainPic(copy.getMainPic());
            dto.setImgs(copy.getImgs());
            dto.setCheckType(copy.getCheckType());
            dto.setNum(copy.getNum());
            dto.setCheckNum(copy.getCheckNum());
            dto.setCompleteNum(copy.getCompleteNum());
            dto.setPrice(copy.getPrice());
            dto.setCheckStatus(copy.getCheckStatus());
            dto.setRefuseReason(copy.getRefuseReason());
            dto.setStatus(copy.getStatus());
            dto.setHasNum(copy.getHasNum());
            dto.setUserId(copy.getUserId());
            dto.setPublishTime(copy.getPublishTime());
            dto.setCtime(copy.getCtime());
            dto.setUtime(copy.getUtime());

            return dto;
        });
    }

//    @RequestMapping("/check")
//    @ResponseBody
//    public FsResponse list(Long id, Integer status, String reason) {
//
//
//        Mission mission = new Mission();
//        Mission srcMIssion = missionService.selectById(id);
//
//        mission.setId(id);
//        //通过
//        if (status == 1) {
//            mission.setCheckStatus(1);
//            mission.setPublishTime(new Date());
//        } else {
//
//            // 返还扣除的佣金和金额
//            UserBalanceDetail detail1 = balanceService.selectDetailByReferId(id, BalanceType.MISSION);
//            balanceService.update(srcMIssion.getUserId(), BalanceType.MISSION_REFUSE, detail1.getMoney(), id, "返还发布任务金额");
//            UserBalanceDetail detail2 = balanceService.selectDetailByReferId(id, BalanceType.MISSION_COMMISSION);
//            balanceService.update(srcMIssion.getUserId(), BalanceType.MISSION_COMMISSION_REFUSE, detail2.getMoney(), id, "返还发布任务佣金");
//            mission.setCheckStatus(2);
//            mission.setRefuseReason(reason);
//        }
//        missionMapper.updateByPrimaryKeySelective(mission);
//        return FsResponseGen.success();
//
//    }

    @RequestMapping("/detail/index")
    public String detail(Long id) {

        getRequest().setAttribute("mission", missionMapper.selectByPrimaryKey(id));
        return "mission/detail";
    }

}
